/*-------------------------------------------------------------------------------------------------*/
/* Nuvoton Corporation confidential                                                    */
/*                                                                                                 */
/* Copyright (c) 2009 by Nuvoton Corporation                                           */
/* All rights reserved                                                                             */
/*                                                                                                 */
/*-------------------------------------------------------------------------------------------------*/
/* File Name:                                                                                      */
/*     emc_test.c                                                                                  */
/* Description:                                                                                    */
/*     This application sends a UDP packet to itself and test the network interface, a loopback    */
/*          connector should be used to create the external loop back environment                  */
/* History:                                                                                        */
/*                                                                                                 */
/* Project:                                                                                        */
/*-------------------------------------------------------------------------------------------------*/


#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#include "wblib.h"

#include "tcp.h"
#include "stats.h"
#include "etharp.h"
#include "ethernetif.h"
#include "memp.h"


#define EMC_TIMEOUT  50


static INT volatile _count = 0;

/* Receive the packet from ethernet, do nothing and drop it */
static VOID _MyRecv(void *arg, struct udp_pcb *pcb, struct pbuf *p, struct ip_addr *addr, u16_t port)
{

    _count++;
    pbuf_free(p);
    return;

}


/****************************************************************************
* FUNCTION
*   _EmcHelp
*
* DESCRIPTION
*   The emc test help function
*
* CALLED BY
*   emc_main/main
*
* INPUTS
*   None
*
* OUTPUTS
*   None
*
* HISTORY
*   NAME        DATE        REMARKS
*
*
* REMARK
*     None
****************************************************************************/
static void _EmcHelp(void)
{
	sysprintf("\nEMC test:\n\n");
	sysprintf("Please make sure RJ45 jack on board is conenct with a loop back connector via CAT5 cable\n");
	sysprintf("Test is passed if EMC can receive a UDP packet it sent out\n");
    sysprintf("Press any key to continue the test...\n");
    
    sysGetChar();	
}	



INT emc_main(VOID)
{

    static struct ip_addr gw, ipaddr, netmask;
    static struct ip_addr sb;
    static struct netif netif;
    static struct udp_pcb *rx_pcb, *tx_pcb;
    static INT flag = 0;
    struct pbuf *tmp_buf;
    UINT8 payload[100];
    UINT32 tick;
    INT i;

    _count = 0;

	_EmcHelp();

    IP4_ADDR(&gw, 192, 168, 0, 1);
    IP4_ADDR(&ipaddr, 192, 168, 0, 228);
    IP4_ADDR(&netmask, 255, 255, 255, 0);

    IP4_ADDR(&sb, 192, 168, 0, 228);
    for (i = 0; i < sizeof(payload); i++)
        payload[i] = 0x55;

    /* Only need to init once. */
    if (flag == 0) {
        stats_init();
        sys_init();

        /* mem_init(); */ /* This function does nothing */
        memp_init();
        pbuf_init();



        etharp_init();
        
		sysprintf("EMC init complete\n");	

        ip_init();
        udp_init();
        tcp_init();


        netif_add(&netif, &ipaddr, &netmask, &gw, NULL, ethernetif_init, ip_input);
        netif_set_default(&netif);
        netif_set_up(&netif);

        /* This is the pcb for Rx */
        rx_pcb = udp_new();
        if (udp_bind(rx_pcb,IP_ADDR_ANY,999) != ERR_OK)
            sysprintf("bind error\n");


        udp_recv(rx_pcb, _MyRecv, NULL);

        /* This is the pcb for Tx */
        tx_pcb = udp_new();

        flag = 1;

    }
	//tick = sysGetTicks(TIMER0);
	//while(sysGetTicks(TIMER0) - tick < EMC_TIMEOUT);

    tmp_buf = pbuf_alloc(PBUF_TRANSPORT, sizeof(payload), PBUF_RAM);
    memcpy(tmp_buf->payload, payload, i);
    udp_sendto(tx_pcb, tmp_buf, &sb, 999);
    sysprintf("Packet sent, wait for 5 second at most\n");	
    pbuf_free(tmp_buf);
	
	tick = sysGetTicks(TIMER0);

	while(sysGetTicks(TIMER0) - tick < (EMC_TIMEOUT * 10)) {
		if(_count != 0) {
		    sysprintf("EMC test pass\n");		
			return(0);
		}
	}

    sysprintf("EMC TEST FAILED\n");
    return(1);

}

